<template>
  <div
    ref="toolbox"
    :class="['float_toolbox_module', isDragging ? 'flutter' : '', 'is-' + direction, 'is-' + size]"
  >
    <header @mousedown="onDragMouseDown">
      <i class="iconfont icon-drag"></i>
    </header>
    <section>
      <slot />
    </section>
  </div>
</template>

<script lang="ts">
import { Dictionary } from '@/interface/Basic'
import Vue from 'vue'
import { Getter } from 'vuex-class'
import { Component, Ref, Prop } from 'vue-property-decorator'
@Component
class WsFloatToolbox extends Vue {
  @Getter('theme') theme!: 'light' | 'dark'
  @Ref() toolbox!: HTMLElement
  @Prop({ default: 30 }) right!: number
  @Prop({ default: 30 }) bottom!: number
  @Prop({ default: 'small' }) size!: 'mini' | 'small' | 'medium'
  @Prop({ default: 'vertical' }) direction!: 'vertical' | 'horizontal'
  cloneEl!: HTMLElement
  isDragging: boolean = false
  parentSize: Dictionary<number> = {}
  created() {
    this.initialPosition = {
      right: this.right,
      bottom: this.bottom
    }
  }
  initialPosition: Dictionary<number> = {
    bottom: 30,
    right: 30
  }
  destroyed() {
    window.removeEventListener('click', () => {
      return
    })
    window.removeEventListener('mousemove', () => {
      return
    })
    window.removeEventListener('mouseup', () => {
      return
    })
    window.removeEventListener('mouseleave', () => {
      return
    })
  }
  // 拖拽功能
  onDragMouseDown() {
    this.init(this.toolbox)
    // 监听鼠标移动事件
    window.addEventListener('mousemove', e => {
      if (this.isDragging) {
        this.computePosition(e)
      }
    })
    window.addEventListener('mouseup', () => {
      this.isDragging = false
      window.removeEventListener('mousemove', () => {
        return
      })
    })
    window.addEventListener('mouseleave', () => {
      this.isDragging = false
    })
  }
  init(e: HTMLElement) {
    this.isDragging = true
    if (!e.parentElement) {
      this.$message({
        type: 'warning',
        message: '未找到父节点'
      })
      return
    }
    const { width, height, left, top } = e.parentElement.getBoundingClientRect()
    const { offsetHeight, offsetWidth } = e
    this.parentSize = {
      parentWidth: width,
      parentHeight: height,
      parentLeft: left,
      parentTop: top
    }
    this.initialPosition = Object.assign(this.initialPosition, {
      width: offsetWidth,
      height: offsetHeight
    })
  }
  computePosition({ pageX: dropX, pageY: dropY }: MouseEvent) {
    // toolbox 参数
    const { height, width, bottom, right } = this.initialPosition
    // 父节点容器尺寸
    const { parentWidth, parentHeight, parentLeft, parentTop } = this.parentSize
    let finalRight = parentWidth - (width + dropX - parentLeft) + 15
    if (finalRight < 0) finalRight = 0
    else if (finalRight > parentWidth - width) finalRight = parentWidth - width
    // 处理 toolbox 纵向极限位置
    let finalBottom = parentHeight - (height + dropY - parentTop) + 15
    if (finalBottom < 0) finalBottom = 0
    else if (finalBottom > parentHeight - height) finalBottom = parentHeight - height
    this.toolbox.animate(
      [
        { bottom: `${bottom}px`, right: `${right}px` },
        { bottom: `${finalBottom}px`, right: `${finalRight}px` }
      ],
      { duration: 200, easing: 'ease', fill: 'both' }
    )
    this.initialPosition.bottom = finalBottom
    this.initialPosition.right = finalRight
  }
  getSizes() {
    return this.toolbox.getBoundingClientRect()
  }
}
export default WsFloatToolbox
</script>

<style lang="less" scoped>
@import '~@/styles/theme.less';
@import '~@/styles/layout.less';
.float_toolbox_module {
  position: absolute;
  box-sizing: border-box;
  padding: 5px;
  border-radius: 5px;
  overflow: hidden;
  transition: all 0.5s ease;
  #flex(column, center, flex-start);
  #color(background-color, --module-bg-color);
  #shadow-color(0, 4px, 6px, 0, --primary-shadow-color);
  i {
    display: inline-block;
    cursor: pointer;
    #color(color, --primary-text-color);
  }
  header > i {
    font-size: 20px;
    cursor: grab;
  }
  section {
    padding-top: 5px;
    border-top: solid 1px;
    #flex(column, center, flex-start);
    i {
      font-size: 18px;
      margin-bottom: 3px;
      transition: all 0.2s ease;
    }
    i:hover {
      #color(color, --lighter-blue-color);
    }
    i:last-child {
      margin-bottom: 0;
    }
  }
  header,
  section,
  header {
    box-sizing: border-box;
    #color(border-color, --primary-border-color);
  }
}
.is-horizontal {
  flex-direction: row;
  header {
    .icon-drag {
      transform: rotate(90deg);
    }
  }
  section {
    flex-direction: row;
    padding: 0;
    padding-left: 5px;
    margin: 0 5px;
    border-top: 0;
    border-left: solid 1px;
    i {
      margin: 0 2px;
    }
  }
  header,
  section {
    #color(border-color, --primary-border-color);
  }
}
.is-small {
  padding: 10px;
  header > i {
    font-size: 24px;
  }
  section > i {
    font-size: 22px;
  }
}
.is-medium {
  padding: 10px;
  header > i {
    font-size: 26px;
  }
  section > i {
    font-size: 24px;
  }
}
.flutter {
  filter: brightness(1);
  cursor: grabbing;
  z-index: 9999;
  .icon-drag {
    cursor: grabbing;
  }
}
.isFold {
  width: 30px;
  .icon-arrow-right {
    transform: rotate(180deg);
  }
}
</style>
